package com.safeneeds.linguimana.entity.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.type.StandardBasicTypes;

import com.safeneeds.linguimana.entity.EntityPortNews;
import com.safeneeds.linguimana.entity.PortNewsCateMap;
import com.safeneeds.linguimana.entity.PortNewsCateMapId;
import com.safeneeds.linguimana.entity.dao.IPortNewsCateMapDao;
import com.safeneeds.util.base.BaseHibernateDao;
import com.safeneeds.util.other.StringUtil;

/**
 * 新闻分类对应关系
 * @author gaof
 * @date 2017年2月9日
 */
public class PortNewsCateMapDao extends BaseHibernateDao<PortNewsCateMap, PortNewsCateMapId> implements IPortNewsCateMapDao {

	/**
	 * 批量添加新闻分类对应关系
	 * @param list
	 */
	@Override
	public void addNewsCateMapList(List<PortNewsCateMap> list) {
		this.saveEntities(list);
	}

	/**
	 * 通过新闻id获取新闻分类
	 * @param newsId
	 * @return
	 */
	@Override
	public List<PortNewsCateMap> getNewsCateByNewsId(String newsId) {
		return this.getEntities("FROM PortNewsCateMap WHERE id.newsId = '" + StringUtil.addQuote(newsId) + "'");
	}

	/**
	 * 通过新闻id删除新闻分类对应关系
	 * @param newsId
	 */
	@Override
	public void delNewsCateMapByNewsId(String newsId) {
		this.executeQuery("delete from port_news_cate_map where news_id = '" + StringUtil.addQuote(newsId) + "'");
	}

	/**
	 * 通过新闻id获取新闻分类信息
	 * @param newsId
	 * @return
	 */
	@Override
	public List getNewsCateMapNameList(String newsId) {
		
		String sql = "SELECT T2.NAME from PORT_NEWS_CATE_MAP T1"
				+ " LEFT JOIN PORT_NEWS_CATE T2 on T2.CATE_ID = T1.CATE_ID"
				+ " WHERE T1.NEWS_ID = '" + StringUtil.addQuote(newsId) + "'";
		
		Map<String, org.hibernate.type.Type> type = new HashMap<String, org.hibernate.type.Type>();
		type.put("NAME", StandardBasicTypes.STRING);
		
		return this.getSQLQuery(sql, type);
	}

}
